'use strict';

/**
 * Created by zhu on 14/12/21.
 */
var common = angular.module('common');

common.constant('PageConstant', {countPerPage: 5});
common.constant('RootPathConstant', '/sumer/api/v1');
common.constant('PathSuffixConstant', '');
//common.constant('RootPathConstant', '/json');
//common.constant('PathSuffixConstant', '.json');
common.constant('ImageTypes', ['jpeg', 'jpg', 'gif', 'png', 'bmp']);
common.constant('PhotoHost', "http://182.92.157.212/");
//common.constant('PhotoHost', "http://resource.zhuclass.com/");

common.service('RouteIdService', ['$routeParams', '$location', function ($routeParams, $location) {
    this.get = function () {
        if ($location.path().indexOf("blog")) {
            return $routeParams.blogId;
        } else if ($location.path().indexOf("photo")) {
            return $routeParams.photoId;
        } else {
            return null;
        }
    };
}]);

common.factory('CurrentUser', ['$cookies', '$window', '$http', 'RootPathConstant', function ($cookies, $window, $http, RootPathConstant) {
    var uId = $cookies.uid,
        uIdPk = $cookies.uidpk,
        userId = $cookies.userid,
        userName = $cookies.username;

    function error() {
        //$window.location.href = 'http://www.zhuclass.com/member/login';
        $window.location.href = 'login.html';
    }

    if (!uId || !uIdPk) {
        error();
    }

    return {
        get: function (success) {
            if (!userId || !userName) {
                $http.get(RootPathConstant + '/auth/userId').success(function (resposne) {
                    $cookies.userid = resposne.identity;
                    $cookies.username = resposne.name;
                    if (success) {
                        success();
                    }
                }).error(error);
            } else {
                if (success) {
                    success();
                }
            }

        }
    };

}]);

common.factory('ThirdPartyPaths', ['RootPathConstant', '$http', function (RootPathConstant, $http) {
    var paths = null;

    return {
        get: function (success) {
            if (paths && success) {
                success(paths);
            }
            $http.get(RootPathConstant + '/thirdparty/paths').success(function (resposne) {
                paths = resposne;
                if (success) {
                    success(resposne);
                }
            });
            return paths;
        }
    };
}]);
